package com.speech.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.speech.R;
import com.speech.net.BasicCallBack;
//import com.speech.net.HttpCommunicationManager;
import com.speech.net.constant.EnumDeviceType;
import com.speech.net.constant.EnumReqestActionCode;
import com.speech.net.entity.MessageC2S;
import com.speech.net.entity.MessageC2SBuilder;
import com.speech.net.entity.MessageS2C;
import com.speech.utils.AESEncryption;
import com.speech.utils.AudioManager;
import com.speech.utils.DeviceUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.speech.utils.JsonProcessor;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends BaseActivity {

    private AudioManager mAudioManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    //    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//
//        mAudioManager = AudioManager.getInstance();
//
//        EasyPermissions.requestPermissions(MainActivity.this,"asdf",123, Manifest.permission.RECORD_AUDIO);
//
//        Button btn = findViewById(R.id.btn_test);
//
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String rst = AESEncryption.decrypt("AFjcOQ6DSOmU3NJ7jwYuu4atD8janoAah9KgoqQzBBrI/b6weT7JfpmYSLsoP4UaLfaT4vPZWEPiZnVb3n9KYg==");
//                Log.d("fzw","rst:"+rst);
//            }
//        });
//
//        final Runnable runnable = new Runnable() {
//            @Override
//            public void run() {
//                try {
//
//                    int i = 0;
//                    while(i<=10) {
//                        Thread.sleep(500L);
//
//                        Log.d("fzw", "voice level:" + mAudioManager.getVoiceLevel(10));
//                        i++;
//                    }
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        };
//
//
//
////        btn.setOnTouchListener(new View.OnTouchListener() {
////            @Override
////            public boolean onTouch(View v, MotionEvent event) {
////
////                switch (event.getAction()){
////                    case MotionEvent.ACTION_DOWN:
////                        Log.d("fzw","down!!!");
////                        mAudioManager.prepareAudio();
////                        new Thread(runnable).start();
////                        break;
////
////                    case MotionEvent.ACTION_UP:
////                        Log.d("fzw","up!!!");
////                        mAudioManager.release();
////                        break;
////                }
////
////                return true;
////            }
////        });
//
////        btn.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////                String s = "BuildacodeAES-128bitEncryption";
//////                String encode = AESEncryption.encrypt(s);
////                String encode = AESEncryption.encrypt(_generate_request_json());
//////                Log.d("fzw-test","encode:"+encode);
////                Log.d("fzw-test","decode:"+AESEncryption.decrypt("R97KGVr4vWWuxyF8m+zdz39+oDTer2Igb9j03oz0I1Zi5xTJCrDbN3JVpT1yKMcgd9nSTEalDOCIt7a78F8Ehz65FVbveKdLJ37Ulu/PAokTs31COPvnZy3hj4HCXkov"));
//////                Log.d("fzw-test","decode:"+AESEncryption.decrypt("RBLayfL9lPP3jg92CqtXlYLjGf8FHklWhFYEW3U0Pdc="));
////            }
////        });
//
////        btn.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View view) {
////                HttpCommunicationManager hcm = HttpCommunicationManager.getInstance();
////                hcm.doPost(_generate_request_json(),new Callback<ResponseBody>() {
////                    @Override
////                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//////                        Log.d("fzw-request","response all:"+response.toString());
////
////                        try {
//////                            Log.d("fzw-request","response body:"+);
////                            String resp = response.body().string();
////                            Log.d("fzw-request",AESEncryption.decrypt(resp));
////                        } catch (IOException e) {
////                            e.printStackTrace();
////                        }
////                    }
////
////                    @Override
////                    public void onFailure(Call<ResponseBody> call, Throwable t) {
////                        Toast.makeText(getApplicationContext(),"fail!!!",Toast.LENGTH_LONG).show();
////                    }
////                });
////            }
////        });
//
//    }

    private String _generate_request_json() {

        MessageC2S msg = new MessageC2S();
        msg.reqestAction = EnumReqestActionCode.设备登记;
        msg.strDeviceID = DeviceUtil.getDeviceID(MainActivity.this);
        msg.enumDeviceType = EnumDeviceType.ANDROID;
        msg.strVersion="1.0.0";

        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(msg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return AESEncryption.encrypt(json);
//        return json;
    }
}
